/**
 * 企业用户管理接口
 *
 * */

import {
  getListZbCompany,
  searchZbCompanyUser,
  editZbCompanyUser,
  deleteZbCompanyUser,
  addZbCompanyUser,
  addCompanyDepartment,
  editCompanyDepartment,
  deleteCompanyDepartment,
  searchCompanyAccountList,
  getCompanyAccountList,
  addCompanyAccount,
  editCompanyAccount,
  deleteCompanyAccount
} from "@/services/companyUserManage";

export default {
  namespace: "companyUserManage",

  state: {
    listZbCompany: [],
    zbCompanyUser: {
      content: []
    },
    companyAccountList: {},
    companyAccount: {}
  },

  effects: {
    *getListZbCompany(_, { call, put, select }) {
      const response = yield call(getListZbCompany);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { listZbCompany: response.data }
      });
    },

    *searchZbCompanyUser({ payload }, { call, put, select }) {
      const response = yield call(searchZbCompanyUser, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { zbCompanyUser: response.data }
      });
    },

    *addZbCompanyUser({ payload }, { call, put, select }) {
      const response = yield call(addZbCompanyUser, payload);
      if (!response) return;
    },

    *editZbCompanyUser({ payload }, { call, put, select }) {
      const response = yield call(editZbCompanyUser, payload);
      if (!response) return;
    },

    *deleteZbCompanyUser({ payload }, { call, put, select }) {
      const { zbCompanyUserId, index } = payload;
      const { zbCompanyUser } = yield select(state => state.companyUserManage);
      const response = yield call(deleteZbCompanyUser, { zbCompanyUserId });
      if (!response) return;
      const newZbCompanyUser = { ...zbCompanyUser };
      newZbCompanyUser.content.splice(index, 1);
      newZbCompanyUser.totalElements -= 1;
      yield put({
        type: "saveState",
        payload: { zbCompanyUser: newZbCompanyUser }
      });
    },

    /******* 部门管理 *******/

    *addCompanyDepartment({ payload }, { call, put, select }) {
      const response = yield call(addCompanyDepartment, payload);
      if (!response) return;
      return response;
    },

    *editCompanyDepartment({ payload }, { call, put, select }) {
      const response = yield call(editCompanyDepartment, payload);
      if (!response) return;
      return response;
    },

    *deleteCompanyDepartment({ payload }, { call, put, select }) {
      const response = yield call(deleteCompanyDepartment, payload);
      if (!response) return;
      return response;
    },

    /******* 账号管理 *******/

    *searchCompanyAccountList({ payload }, { call, put, select }) {
      const response = yield call(searchCompanyAccountList, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { companyAccountList: response.data }
      });
    },

    *getCompanyAccountList({ payload }, { call, put, select }) {
      const response = yield call(getCompanyAccountList, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { companyAccount: response.data }
      });
    },

    *addCompanyAccount({ payload }, { call, put, select }) {
      const response = yield call(addCompanyAccount, payload);
      if (!response) return;
      return response;
    },

    *editCompanyAccount({ payload }, { call, put, select }) {
      const response = yield call(editCompanyAccount, payload);
      if (!response) return;
      return response;
    },

    *deleteCompanyAccount({ payload }, { call, put, select }) {
      const response = yield call(deleteCompanyAccount, payload);
      if (!response) return;
      return response;
    }
  },

  reducers: {
    saveState(state, action) {
      return {
        ...state,
        ...action.payload
      };
    }
  }
};
